<template>
	<view v-if="!commentList || commentList.length === 0" class="noData">
		<up-button type="warning" @click="goHome">暂无评价记录，去商城逛逛</up-button>
	</view>
	<view v-else class="comment-list">
		<view class="comment-item" v-for="(item, index) in commentList" :key="index">
			<view class="goods-info" @click="goGoodsDetail(item.goods_id)">
				<image :src="baseUrl + '/' + item.goods_img" mode="aspectFill"></image>
				<view class="info">
					<view class="name">{{item.goods_name}}</view>
					<view class="score">
						<uni-rate :value="item.score" disabled disabledColor="#ffca3e" size="15" />
					</view>
				</view>
			</view>
			<view class="comment-content">
				<view class="content">{{item.content}}</view>
				<view class="comment-footer">
					<view class="time">{{item.create_time}}</view>
					<view class="delete" @click="deleteComment(item.id)">删除</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		ref,
		onMounted
	} from 'vue'
	import {
		onShow
	} from '@dcloudio/uni-app'
	import {
		useUserStore
	} from '@/store/index'
	import {
		get,
		post,
		del,
		baseUrl
	} from '@/hooks/req'

	const useUserInfo = useUserStore()
	const commentList = ref([])

	// 加载评价列表
	const loadComments = () => {
		get(`/comment/user/${useUserInfo.userInfo.id}`, uni.getStorageSync("token")).then(res => {
			if (res.data.status === 200) {
				commentList.value = res.data.data || []
			} else {
				commentList.value = []
				uni.showToast({
					title: res.data.message || '获取评价失败',
					icon: 'none'
				})
			}
		})
	}

	// 删除评价
	const deleteComment = (commentId) => {
		uni.showModal({
			content: "确定删除该评价吗？",
			success(res) {
				if (res.confirm) {
					del(`/comment/delete/${commentId}`, uni.getStorageSync("token")).then(res => {
						if (res.data.status === 200) {
							uni.showToast({
								title: "删除成功",
								icon: "none"
							})
							loadComments()
						}
					})
				}
			}
		})
	}

	// 跳转商品详情
	const goGoodsDetail = (goodsId) => {
		uni.navigateTo({
			url: '/pages/goodsDetail/goodsDetail?goodsId=' + goodsId
		})
	}

	// 返回首页
	const goHome = () => {
		uni.switchTab({
			url: '/pages/home/home'
		})
	}

	onShow(() => {
		loadComments()
	})

	onMounted(() => {
		loadComments()
	})
</script>

<style lang="scss" scoped>
	.noData {
		display: flex;
		justify-content: center;
		align-items: center;
		height: 500rpx;
	}

	.comment-list {
		padding: 20rpx;

		.comment-item {
			background-color: #fff;
			border-radius: 20rpx;
			padding: 20rpx;
			margin-bottom: 20rpx;
			box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.1);

			.goods-info {
				display: flex;
				padding-bottom: 20rpx;
				border-bottom: 1px solid #eee;

				image {
					width: 120rpx;
					height: 120rpx;
					border-radius: 10rpx;
				}

				.info {
					flex: 1;
					margin-left: 20rpx;

					.name {
						font-size: 28rpx;
						color: #333;
						font-weight: bold;
						margin-bottom: 10rpx;
					}

					.score {
						display: flex;
						align-items: center;
					}
				}
			}

			.comment-content {
				padding: 20rpx 0;

				.content {
					font-size: 28rpx;
					color: #666;
					line-height: 1.5;
					margin-bottom: 20rpx;
				}

				.comment-footer {
					display: flex;
					justify-content: space-between;
					align-items: center;
					font-size: 24rpx;

					.time {
						color: #999;
					}

					.delete {
						color: #ff6700;
						padding: 10rpx 20rpx;
						border: 1px solid #ff6700;
						border-radius: 30rpx;

						&:active {
							opacity: 0.7;
						}
					}
				}
			}
		}
	}
</style>